package com.demo.controller;


import com.demo.entity.Medicine;
import com.demo.response.Result;
import com.demo.service.IMedicineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 预防药物表 前端控制器
 *
 * @author ZM
 * @since 2020-11-20
 */
@RestController
@RequestMapping("/medicine")
@Api(tags = "预防药物管理类")
public class MedicineController {
    @Autowired
    IMedicineService medicineService;

    @ApiOperation("新增预防药物")
    @PostMapping("/addMedicine")
    public Result addMedicine(@Validated @RequestBody Medicine medicine, BindingResult result) {
        if (result.hasErrors()) {
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return medicineService.addMedicine(medicine);
    }

    @ApiOperation("编辑预防药物")
    @PostMapping("/updateMedicine")
    public Result updateMedicine(@RequestBody Medicine medicine, BindingResult result) {
        if (result.hasErrors()) {
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return medicineService.updateMedicine(medicine);
    }

    @ApiOperation("删除预防药物")
    @GetMapping("/deleteMedicine")
    public Result deleteMedicine(@RequestParam(name = "medicineId") Integer medicineId,
                                 @RequestParam(name = "deletedUserId") Integer deletedUserId) {
        return medicineService.deleteMedicine(medicineId, deletedUserId);
    }

    @ApiOperation("预防药物列表")
    @GetMapping("/medicineList")
    public Result medicineList(@RequestParam(name = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                               @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                               @RequestParam(name = "medicineId", required = false) Integer medicineId,
                               @RequestParam(name = "medicineName", required = false) String medicineName,
                               @RequestParam(name = "allItem", required = false, defaultValue = "false") Boolean allItem) {
        return medicineService.medicineList(pageNumber, pageSize, medicineId, medicineName, allItem);
    }


}
